package hhm.android.base.adapter

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

abstract class HBaseAdapter<F : RecyclerView.ViewHolder, T>(var list: ArrayList<T>) : RecyclerView.Adapter<F>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): F {
        return bindView(parent, viewType)
    }

    override fun onBindViewHolder(holder: F, position: Int) {
        bindData(holder, list[position], position)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun setData(l: ArrayList<T>) {
        list = l
        notifyDataSetChanged()
    }

    fun addData(t: ArrayList<T>) {
        list.addAll(t)
        notifyItemInserted(itemCount)
        notifyItemChanged(itemCount - t.size - 1)
    }

    fun addData(index: Int, t: ArrayList<T>) {
        list.addAll(index, t)
        notifyItemRangeInserted(0, t.size)
        notifyItemChanged(t.size, t.size + 1)
    }

    fun removeData(index: Int) {
        list.removeAt(index)
        notifyItemRemoved(index)
    }

    abstract fun bindView(parent: ViewGroup, viewType: Int): F
    abstract fun bindData(holder: F, bean: T, position: Int)
}